# coding=utf-8

from django.db import models
from user.models import User
from plantform.task_config import TASK_CONFIG


class DataSource(models.Model):
    source_name = models.CharField(max_length=100, default="默认数据源", verbose_name="数据源名字")
    data_source_type = models.CharField(default="db", max_length=200, verbose_name="数据源类型")
    # 与数据库相关的
    db_type = models.CharField(max_length=100, default="mongodb", verbose_name="数据库的类型")
    db_uri = models.CharField(max_length=200, default="mongodb://127.0.0.1:27017", verbose_name="数据库URI")
    db_name = models.CharField(max_length=100, default="kd_plantform", verbose_name="数据库的名字")
    db_form = models.CharField(max_length=200, default="default", verbose_name="数据表名字")
    db_host = models.GenericIPAddressField(default='127.0.0.1', verbose_name="数据库IP地址")
    db_port = models.IntegerField(default=3306, verbose_name="数据库端口地址")
    db_user = models.CharField(default="root", max_length=100, verbose_name="数据库用户名")
    db_pwd = models.CharField(default="", max_length=200, null=True, verbose_name="数据库密码")
    db_fields = models.CharField(max_length=200, default="html", verbose_name="载入数据的字段名，多个以，隔开")
    # hdfs
    hdfs = models.CharField(max_length=200, default="", null=True, verbose_name="hdfs的路径")
    # txtfile
    txt_file_type = models.CharField(max_length=100, default="txt", verbose_name="文本文件的类型")
    txt_file_path = models.CharField(max_length=200, default=TASK_CONFIG['DataSourceInputConf']['DataSourceLocation'],
                                     null=True, verbose_name="文本文件的位置")
    txt_file_delimiter = models.CharField(max_length=100, default=",", verbose_name="文件内容分隔符")
    # status 0表示未测试 1表示测试成功 2表示测试失败
    data_source_status = models.IntegerField(default=0, verbose_name="数据源可用状态")
    # 加入时间
    add_datetime = models.DateTimeField(auto_now=True, verbose_name="数据源加入时间")
    # 加入人
    user = models.ForeignKey(User, max_length=200, on_delete=models.CASCADE, verbose_name="操作人")
    limit_count = models.IntegerField(default=0, verbose_name="载入数据的数据量，0为所有数据")

    class Meta:
        verbose_name = '数据源配置'
        verbose_name_plural = verbose_name
        ordering = ("-id", "-add_datetime")


class OutDataSource(models.Model):
    source_name = models.CharField(max_length=100, default="默认数据源", verbose_name="数据源名字")
    output_type = models.CharField(max_length=100, default="console", verbose_name="输出的类型")
    text_path = models.CharField(max_length=200, default="", null=True, verbose_name="txt文本存入的路径")
    hdfs_path = models.CharField(max_length=200, default="", null=True, verbose_name="如果是hdfs，存入的路径")
    # 如果选择的是 db
    database_type = models.CharField(max_length=100, default="mongodb", verbose_name="数据库的类型")
    database_name = models.CharField(max_length=100, default="kd_ml_output", verbose_name="数据库名")
    database_host = models.GenericIPAddressField(default="127.0.0.1", verbose_name="数据库的IP")
    database_port = models.IntegerField(default=27017, verbose_name="数据库的端口号")
    database_table = models.CharField(max_length=100, default="ml_results", verbose_name="数据库的表名")
    database_user = models.CharField(max_length=100, default="root", verbose_name="数据库的用户")
    database_pwd = models.CharField(max_length=100, default="", null=True, verbose_name="数据库的密码")
    # 其他相关
    add_datetime = models.DateField(auto_now=True, verbose_name="添加的时间")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="添加的人")
    output_source_status = models.IntegerField(default=0, verbose_name="源是否可用")

    class Meta:
        verbose_name = '输出源配置'
        verbose_name_plural = verbose_name
        ordering = ("-id", "-add_datetime")


class PlantformConfig(models.Model):
    # 对应的是基本的spark平台的appname
    app_name = models.CharField(max_length=100, default="kd_ml", verbose_name="Spark平台的APP的名字")
    spark_master = models.CharField(max_length=100, default="local[*]", verbose_name="Spark集群的地址")

    # 数据处理相关的配置
    zh_support = models.BooleanField(default=False, verbose_name="中文支持")
    allow_number = models.BooleanField(default=False, verbose_name="纯数字支持")
    split_limit_count = models.IntegerField(default=2, verbose_name="中文切分最小文字数量")
    fast_stop_words = models.CharField(default="", null=True, max_length=200, verbose_name="快速去除词，多个以空格隔开")

    # transform的相关配置
    transform_type = models.CharField(default="CountVector", max_length=100, verbose_name="特征转换的选取")
    input_col = models.CharField(default="words", max_length=100, verbose_name="特征转化的输入col")
    output_col = models.CharField(default="feature", max_length=100, verbose_name="特征转换输出的col")

    # 数据算法相关配置
    ml_type = models.CharField(default="LDA", max_length=100, verbose_name="算法选择的类型")
    ml_params = models.CharField(default="", max_length=200, verbose_name="各种算法对应的基本配置参数，以json字符串给定")

    # 其他相关
    add_datetime = models.DateField(auto_now=True, verbose_name="添加的时间")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="添加的人")

    class Meta:
        verbose_name = '数据挖掘建模配置'
        verbose_name_plural = verbose_name
        ordering = ("-id", "-add_datetime")


class Task(models.Model):
    data_source = models.ForeignKey(DataSource, on_delete=models.CASCADE, verbose_name="分析使用的数据源")
    output_data_source = models.ForeignKey(OutDataSource, on_delete=models.CASCADE, verbose_name="输出源的配置")
    ml_config = models.ForeignKey(PlantformConfig, on_delete=models.CASCADE, verbose_name="进行分析的所有配置")
    # 其他相关
    add_datetime = models.DateField(auto_now=True, verbose_name="添加的时间")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="添加的人")
    # 然后是与任务执行状态相关
    task_status = models.IntegerField(default=0, verbose_name="任务执行情况")
    task_results = models.TextField(default="", null=True, verbose_name="任务执行的基本状况")
    end_datetime = models.DateField(verbose_name="任务执行完成时间", auto_now=True)
    error_info = models.CharField(default="internal error", max_length=200, null=True, verbose_name="任务错误原因")
    celery_id = models.CharField(default="", max_length=200, null=True, verbose_name="异步执行任务ID")
    task_name = models.CharField(default="task_name", max_length=255, verbose_name="任务名称")
    task_description = models.CharField(default="",max_length=255, verbose_name="任务剪短描述")
    report_keyword = models.CharField(default="", max_length=100, verbose_name="关键词报告", null=True, blank=True)

    class Meta:
        verbose_name = '任务执行状态'
        verbose_name_plural = verbose_name
        ordering = ("-id", "-add_datetime")


class TaskReport(models.Model):
    word_cloud = models.TextField(verbose_name="词云数据")
    bar_data = models.TextField(verbose_name="柱状图")
    line_data = models.TextField(verbose_name="折线图")
    pie_data = models.TextField(verbose_name="拼装图")
    relation_data = models.TextField(verbose_name="关系图")
    sunburst_data = models.TextField(verbose_name="旭日图")
    task_id = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name="外键任务关联")
    text_data = models.TextField(verbose_name="文字填充",default="")
